package com.itlong.cloud.controller.old;

import com.itlong.cloud.POJO.DTO.smartcard.SmartCardOldUserInfoDTO;
import com.itlong.cloud.POJO.PO.ProjectPO;
import com.itlong.cloud.POJO.VO.smartcard.SmartCardOldUserInfoVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.enumerate.oldapp.Code;
import com.itlong.cloud.enumerate.oldapp.Message;
import com.itlong.cloud.project.operate.IOperateProjectServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.smartcard.ISmartCardOldDeviceServiceClient;
import com.itlong.cloud.user.smartcard.ISmartCardOldUserInfoServiceClient;
import com.itlong.cloud.utils.words.NumberHandlerUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      旧一卡通的用户模块。
 * <desc/>
 *
 * @createDate 2017/11/2.
 */
@RestController
@RequestMapping("/sys/user")
public class SmartCardOldUserInfoController {

    private static final Logger LOG = LoggerFactory.getLogger(SmartCardOldUserInfoController.class);

    @Autowired
    ISmartCardOldUserInfoServiceClient iSmartCardOldUserInfoServiceClient;
    @Autowired
    ISmartCardOldDeviceServiceClient iSmartCardOldDeviceServiceClient;
    @Autowired
    IOperateProjectServiceClient iOperateProjectServiceClient;

    /**
      * <desc>
      *      旧一卡通_获取用户信息列表。
      * <desc/>
      *
      * @param  myUserName 当前登录账号的用户名
      * @return phoneNo 手机号码
      * @author shengen.T
      * @createDate 2017/11/2
      */
    @LoggerInfo(operateDesc = "旧一卡通_获取用户信息列表")
    @ResponseBody
    @RequestMapping(path = "/manage/list",method = RequestMethod.POST)
    public Message getUserInfoList(String myUserName,String phoneNo,String token){
       Message messageVO;
       try {
           Integer totalNum = 0;
           //判断用户名
           if(!PlatformConstants.OLD_SMARTCARD_LOGIN_USER.equals(myUserName)){
              return new Message(Code.LOGIN_ERROR);
           }
           List<SmartCardOldUserInfoVO> userInfos = iSmartCardOldUserInfoServiceClient.getUserInfoList(phoneNo);
           Map<String,Object> map = new HashMap<>();
           map.put("userInfos",userInfos);
           if((userInfos!=null)&&(userInfos.size()>0)){
               totalNum = 1;
           }
           map.put("totalNumber",totalNum);
           messageVO = new Message(Code.SUCCESS,map);
       }catch (Exception e){
           throw new DataAccessException("【旧一卡通getOne】_获取用户信息列表失败", e);
       }
       return messageVO;
    }

     /**
      * <desc>
      *       一卡通检查手机号是否存在。
      * <desc/>
      *
      * @param  phoneNo 手机号码
      * @return
      * @author shengen.T
      * @createDate 2017/11/3
      */
    @LoggerInfo(operateDesc = "旧一卡通检查手机号是否存在")
    @RequestMapping(path = "check/phone", method = RequestMethod.POST)
    public Message checkPhone( String phoneNo,String token){
        Message message;
        try {
            //判断手机号码
            if(!NumberHandlerUtil.isPhoneNo(phoneNo)){
                return new Message(Code.PHONE_NO_INVALID_ERR);
            }
            boolean result = iSmartCardOldUserInfoServiceClient.checkPhone(phoneNo);
            if(!result){
                return new Message(Code.SUCCESS);
            }
            //存在该手机号码
            message = new Message(Code.EXIST_PHONENO);
        }catch (Exception e) {
            throw new DataAccessException("【旧一卡通checkPhone】检测手机号是否存在失败", e);
        }
        return  message;
    }


    /**
     * <desc>
     *      旧一卡通_修改用户。
     * <desc/>
     *
     * @param updateDTO 更新数据
     * @return 返回影响行数
     * @author shengen.T
     * @createDate 2017/11/3
     */
    @LoggerInfo(operateDesc = LogDescConstant.UPDATE_OLDSMARTCARD_USERINFO_LOG_DESC)
    @ResponseBody
    @RequestMapping(path = "manage/update", method = RequestMethod.POST)
    public Message update(SmartCardOldUserInfoDTO updateDTO){
        Message message;
        try {
            //判断登录名
            if(StringUtils.isBlank(updateDTO.getMyUserName()) ||
                    !(PlatformConstants.OLD_SMARTCARD_LOGIN_USER).equals(updateDTO.getMyUserName())){
                    return new Message(Code.NOT_FOUNDUSER);
            }
            //判断软卡号
//            if(StringUtils.isBlank(updateDTO.getCardNum()) ||
//                    (!updateDTO.getCardNum().matches("[0-9]{6}"))){
//                return new Message(Code.CARD_NUM_INVALID_ERR);
//            }
            //判断手机号
            if(!NumberHandlerUtil.isPhoneNo(updateDTO.getUpdatePhoneNo())){
                return new Message(Code.PHONE_NO_INVALID_ERR);
            }
            //判断用户状态
            Integer userStatus = StringHandlerUtil.strToIntDef(updateDTO.getUserStatus(),-1);
            if((null==userStatus)||(userStatus != DataStatusEnum.NORMAL.getType() &&
                    userStatus != DataStatusEnum.DELETE.getType())){
                return new Message(Code.USER_STATUS_INVALID_ERR);
            }

            //获取项目信息
            ProjectPO projectPO = iOperateProjectServiceClient.getOneProject(updateDTO.getProjectId());
            if (projectPO == null){
                return new Message(Code.PROJECT_NOT_EXIST);
            }
            updateDTO.setIsSmartCommunity(projectPO.getIsSmartCommunity());
            updateDTO.setProjectName(projectPO.getProjectName());
            Integer count =iSmartCardOldUserInfoServiceClient.update(updateDTO);
            message = new Message(Code.SUCCESS, count);
        }catch (Exception e) {
            throw new DataAccessException("【旧一卡通update】修改用户失败", e);
        }
        return  message;
    }


}
